package com.sharer.last.controller;

import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.model.dto.category.CategoryChangeDTO;
import com.sharer.last.model.dto.category.CategoryQueryDTO;
import com.sharer.last.model.vo.category.CategoryVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sharer.last.annotation.OperationLogger;
import com.sharer.last.service.CategoryService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客分类后台管理
 *
 * @ClassName CategoryAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:03
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/category")
public class CategoryAdminController {

    @Resource
    CategoryService categoryService;

    /**
     * 获取博客分类列表
     *
     * @param queryDTO 请求参数
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<CategoryVO>> page(@RequestBody CategoryQueryDTO queryDTO) {
        PageVO<CategoryVO> pageList = categoryService.selectListByPage(queryDTO);
        return R.success(pageList);
    }

    /**
     * 获取博客分类列表
     *
     * @return
     */
    @GetMapping("/selectAllList")
    public R<List<CategoryVO>> selectAllList() {
        List<CategoryVO> list = categoryService.selectAllList();
        return R.success(list);
    }

    /**
     * 添加新分类
     *
     * @param dto 分类实体
     * @return
     */
    @OperationLogger("添加分类")
    @PostMapping("/create")
    public R create(@RequestBody @Validated CategoryChangeDTO dto) {
        categoryService.createCategory(dto);
        return R.handle();
    }

    /**
     * 修改分类名称
     *
     * @param dto 分类实体
     * @return
     */
    @OperationLogger("修改分类")
    @PutMapping("/update")
    public R update(@RequestBody @Validated CategoryChangeDTO dto) {
        categoryService.updateCategory(dto);
        return R.handle();
    }

    /**
     * 按id删除分类
     *
     * @param id 分类id
     * @return
     */
    @OperationLogger("删除分类")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        categoryService.deleteCategoryById(id);
        return R.handle();
    }
}
